package com.sky.utils;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sky.exception.OrderBusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class MapUtil {

    @Value("${sky.shop.address}")
    private String shopAddress;

    @Value("${sky.baidu.ak}")
    private String ak;

    /**
     * 是否超出范围
     */
    public Boolean checkOutOfRange(String address) {
        // 获取店铺坐标
        Map map = new HashMap();
        map.put("address", shopAddress);
        map.put("output", "json");
        map.put("ak", ak);
        String shopLocation = HttpUtil.get("https://api.map.baidu.com/geocoding/v3/", map);
        JSONObject jsonObject = JSON.parseObject(shopLocation);
        // {"status":0,"result":{"location":{"lng":118.12560437797139,"lat":24.480702850878},"precise":1,"confidence":70,"comprehension":100,"level":"地产小区"}}
        log.info("店铺地址解析结果:{}", jsonObject.toString());
        if (!jsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("店铺地址解析异常");
        }
        // 根据解析结果，获取经纬度坐标
        JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
        // 经度
        String lng = location.getString("lng");
        // 纬度
        String lat = location.getString("lat");
        String lngLat = lat + "," + lng;

        // 获取用户收货地址坐标
        map.put("address", address);
        String userLocation = HttpUtil.get("https://api.map.baidu.com/geocoding/v3/", map);
        JSONObject userJsonObject = JSON.parseObject(userLocation);
        // {"status":0,"result":{"location":{"lng":118.12560437797139,"lat":24.480702850878},"precise":1,"confidence":70,"comprehension":100,"level":"地产小区"}}
        log.info("用户收货地址解析结果:{}", userJsonObject.toString());
        if (!userJsonObject.getString("status").equals("0")) {
            throw new OrderBusinessException("用户收货地址解析结果异常");
        }
        // 根据解析结果，获取经纬度坐标
        JSONObject userlngLat = userJsonObject.getJSONObject("result").getJSONObject("location");
        // 经度
        String userLng = userlngLat.getString("lng");
        // 纬度
        String userLat = userlngLat.getString("lat");
        String user = userLat + "," + userLng;

        map.put("origin", lngLat);
        map.put("destination", user);


        // 用百度地图规划的路线计算距离
        String driving = HttpUtil.get("https://api.map.baidu.com/directionlite/v1/driving", map);
        log.info("路线规划结果：{}", driving);
        // 获取规划路线的距离
        JSONObject result = JSON.parseObject(driving);
        if (!result.getString("status").equals("0")) {
            throw new OrderBusinessException("规划路线解析结果异常");
        }
        JSONArray routes = (JSONArray) result.getJSONObject("result").get("routes");
        JSONObject distanceObject = (JSONObject) routes.get(0);
        Integer distance = distanceObject.getInteger("distance");

        // 判断距离是否超出配送范围
        if (distance > 5000) {
            return false;
        } else {
            return true;
        }
    }
}
